/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.MemberScore;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.MemberScoreService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.boxin.ims.modules.wechat.utils.ScoreUtils;

/**
 * 微信会员Controller
 * @author Jakemanse
 * @version 2013-09-08
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/wechatMember")
public class WechatMemberController extends BaseController {

	@Autowired
	private WechatMemberService wechatMemberService;
	
	@Autowired
	private MemberScoreService memberScoreService;
	
	@ModelAttribute
	public WechatMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return wechatMemberService.get(id);
		}else{
			return new WechatMember();
		}
	}
	
	@RequiresPermissions("wechat:wechatMember:view")
	@RequestMapping(value = {"list", ""})
	public String list(WechatMember wechatMember, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			wechatMember.setUser(user);
		}
		
		if(wechatMember.getStartTime() != null){
			wechatMember.setCreateTime(wechatMember.getStartTime());
		}else{
			wechatMember.setCreateTime(null);
			
		}
		
		
        Page<WechatMember> page = wechatMemberService.find(new Page<WechatMember>(request, response), wechatMember); 
        model.addAttribute("page", page);
        model.addAttribute("wechatMember", wechatMember);
		return "modules/wechat/wechatMemberList";
	}

	@RequiresPermissions("wechat:wechatMember:view")
	@RequestMapping(value = "form")
	public String form(WechatMember wechatMember, Model model) {
		model.addAttribute("wechatMember", wechatMember);
		return "modules/wechat/wechatMemberForm";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午10:00:56
	 * @function <p>  微信会员积分充值</p>
	 * @param wechatMember
	 * @param model
	 * @return
	 */
	@RequiresPermissions("wechat:wechatMember:view")
	@RequestMapping(value = "cform")
	public String chargeForm(WechatMember wechatMember, Model model) {
		model.addAttribute("wechatMember", wechatMember);
		return "modules/wechat/wechatMemberChargeScore";
	}

	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午9:55:39
	 * @function <p> 充值积分 </p>
	 * @param wechatMember
	 * @param request
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("wechat:wechatMember:edit")
	@RequestMapping(value = "saveScore")
	public String saveScore(WechatMember wechatMember,HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		
		String chargeScore = request.getParameter("chargeScore");
		wechatMember = wechatMemberService.get(wechatMember.getId());
		if(wechatMember!=null){
			wechatMember.setScore(Integer.parseInt(chargeScore)+wechatMember.getScore());
			ScoreUtils.addWechatMemberChargeScore(wechatMember, Integer.parseInt(chargeScore));
		}
		
		wechatMemberService.save(wechatMember);
		addMessage(redirectAttributes, "微信会员积分充值" + chargeScore + "分成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/wechatMember/?repage";
	}
	
	
	@RequiresPermissions("wechat:wechatMember:edit")
	@RequestMapping(value = "save")
	public String save(WechatMember wechatMember,HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wechatMember)){
			return form(wechatMember, model);
		}
		
		wechatMemberService.save(wechatMember);
		addMessage(redirectAttributes, "保存微信会员'" + wechatMember.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/wechatMember/?repage";
	}
	
	@RequiresPermissions("wechat:wechatMember:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		wechatMemberService.delete(id);
		addMessage(redirectAttributes, "删除微信会员成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/wechatMember/?repage";
	}

}
